import Vue from 'vue';
import Vuex from 'vuex';

import router from '@/router';

Vue.use(Vuex);
Vue.config.devtools = true;

let _this = Vue.prototype;

export default new Vuex.Store({
  state: {
    user: sessionStorage.getItem('user') || '',
    token: sessionStorage.getItem('token') || '',
    userName: sessionStorage.getItem('userName') || '',
    deptMap: {}
  },
  getters: {},
  mutations: {
    setUser(state, user) {
      state.user = user.admin;
      state.userName = user.userName;
      state.token = user.token;
    },
    // 退出登录
    logout(state, user) {
      _this.tools.delA(2);
      router.replace('/login');
      state.user = '';
      state.userName = '';
      state.token = '';
    },
    setDeptMap(state, dept) {
      state.deptMap = dept;
    }

  },
  actions: {
    setUserA(context, user) {
      // console.log(user)
      context.commit('setUser', user);
    },

    async getDeptMapAction(context, dept) {
      return;
      const res = await _this.$api.basics.getDepartmentList();
      let { data } = res.data;
      // console.log(data);
      let a = data.reduce((prev, tar) => {
        prev[tar.deptCode] = tar.deptName;
        return prev;
      }, {});

      // console.log(a);
      context.commit('setDeptMap', a);
    },

    // 暂停之后启动任务
    async startNoticeAfterSuspend(context, id) {
      await _this.$api.notice.getNoticeStartAfterSuspend({
        noticeId: id
      });
    }

  },
  modules: {}
});
